/**
 * @class   JSpaceWrappers
 * @brief   
 ****************************************************************************
 * @version $Id: JSpaceWrappers.h 2891 2010-08-12 01:58:59Z alex $
 * @author  Alessandro Polo
 * @date    Released on 2009-06-04
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_DEVICES_JSpaceWrappers_H__
 #define __WOSH_DEVICES_JSpaceWrappers_H__

 #include <directfb.h>
#include <math.h>


/**************************************
* Local macros/typedef
**************************************/
#define DEFAULT_FNT      "./decker.ttf"

#define WIN_W            856
#define WIN_H            240

#define BAN_W            WIN_W
#define BAN_H            40


#define DFBCHECK(x...)                           \
do {                                             \
  DFBResult err;                                 \
  err = x;                                       \
  if (err != DFB_OK) {                           \
    printf("=> Fail!! err!=DFB_OK, %s", DirectFBErrorString(err) );               \
  }                                              \
} while(0);


#define MAXSHADES        8

#define ABS(x)           ( (x>=0)? x :-x )
#define MIN(x,y)         ( (x<y) ? x : y ) 
#define MAX(x,y)         ( (x>y) ? x : y ) 



typedef struct ColorRGB {
    unsigned char r;
    unsigned char g;
    unsigned char b;
} ColorRGB;

#define R5G6B5VAL(r,g,b)              ( (((r>>3)&0x1f)<<11) | (((g>>2)&0x3f)<<5) | (((b>>3)&0x1f)<<0) )
#define R4G4B4A4VAL(r,g,b)            (0xC000 | (((r>>4)&0x0f)<<8) | (((g>>4)&0x0f)<<4) | (((b>>4)&0x0f)<<0) )
//#define RGBVAL R5G6B5VAL
#define RGBVAL R4G4B4A4VAL


/***************************************
* Local data
***************************************/

static int bAngle=0;
static int bTick = 0;
static unsigned short shades[MAXSHADES][256];
static unsigned short bannerData[BAN_W*BAN_H];

static int fsin1[2048];
static int fsin2[2048]; 
static int fsin3[2048];
static int fcos1[256];

/***************************************
* Local procedures
***************************************/
static void MakeColors(unsigned short cols[], ColorRGB c0, ColorRGB c1, int n0, int n1) {
  int i;
  int cr,cg,cb;
  int di = n1 - n0 + 1;

  for (i=0; i<=di; i++){
    cr = c0.r + ((c1.r-c0.r)*i)/di;
    cg = c0.g + ((c1.g-c0.g)*i)/di;
    cb = c0.b + ((c1.b-c0.b)*i)/di;
    cols[n0+i]= RGBVAL(cr,cg,cb);
  }
}


static void Plasma(unsigned short *buffer, int width, int height, unsigned short shade[]) {
    unsigned int *buf32 = (unsigned int*)buffer;
    unsigned int k = bTick%438;
    unsigned int x, y;
    for (x=0; x<height; x++) {
        for (y=0; y<width; y+=8) {
            *buf32++ = shade[(fsin1[x] + fsin2[y] + fsin3[x+y+k]) & 255] | (shade[(fsin1[x] + fsin2[y+1] + fsin3[x+y+k+1]) & 255]<<16);
            *buf32++ = shade[(fsin1[x] + fsin2[y+2] + fsin3[x+y+k+2]) & 255] | (shade[(fsin1[x] + fsin2[y+3] + fsin3[x+y+k+3]) & 255]<<16);
            *buf32++ = shade[(fsin1[x] + fsin2[y+4] + fsin3[x+y+k+4]) & 255] | (shade[(fsin1[x] + fsin2[y+5] + fsin3[x+y+k+5]) & 255]<<16);
            *buf32++ = shade[(fsin1[x] + fsin2[y+6] + fsin3[x+y+k+6]) & 255] | (shade[(fsin1[x] + fsin2[y+7] + fsin3[x+y+k+7]) & 255]<<16);
        }
    }
    bTick += 1;
}


class jsDisplayer
 {
	public:
		jsDisplayer();
		~jsDisplayer();

	public:
	//	bool waitForPlayer( unsigned long timeout = 0, bool* optional_semaphore = NULL);
		void initialize();
		void enterLoop( bool* optional_semaphore = NULL );

	protected:
		void createFont();
		void setupWindow();
		void plasmaInit();

	protected:
		void clearBG();

	public:
		IDirectFB* dfb;
		IDirectFBEventBuffer* events;

	protected:
		IDirectFBDisplayLayer* layer;
		DFBWindowDescription wdesc;
		IDirectFBWindow* gwindow;
		IDirectFBSurface* gsurface;
		DFBFontDescription fontdesc;
		IDirectFBFont* font;

		char fontname[128];
		char text[256];
		int tw,th;
		int bY;
		DFBRectangle rect;
		DFBRegion update;
		int bOldY, bOldH; 
		int quit;

 };


#endif //__WOSH_DEVICES_JSpaceWrappers_H__
